const request = require("request");
const cheerio = require("cheerio");
const { creteTitle } = require("../model/titleModel");
const { creteParagraph } = require("../model/paragraphMode");
const fetch = (url) =>
  new Promise((resolve, reject) => {
    request(url, (err, response, body) => {
      if (!err && response.statusCode == 200) {
        resolve(body);
      } else {
        reject(err);
      }
    });
  });
// 将字符串转成数组
const convertStrToArr = (str) => {
  let l = 0,
    r = 0,
    n = str.length;
  const separates = [".", "。"];
  const result = [];
  while (r < n) {
    r++;
    if (separates.includes(str[r])) {
      result.push(str.slice(l, r + 1).trim());
      l = r + 1;
    }
  }
  return result;
};

const crawler = async (url) => {
  const url2 = url.replace(/.html/, "_1.html");
  Promise.all([fetch(url), fetch(url2)])
    .then((res) => {
      const title = cheerio.load(res[0]);
      const $ = cheerio.load(res[1]);
      const content = $(".content p");
      const id = new Date().getTime();

      const englishTitle = title("title")
        .text()
        .split("-")
        .splice(1)
        .join("")
        .trim();
      const chineseTitle = $("title")
        .text()
        .split("-")
        .splice(1)
        .join("")
        .trim();
      const author = $(".content p:last").children()[0].prev.data;
      // 第一段信息
      const p1Englisth = $(".content").children()[0].prev.data;
      const p1Chinese = $(".content").children()[0].next.data;
      const englishArr = convertStrToArr(p1Englisth);
      const chineseArr = convertStrToArr(p1Chinese);
      const result = [];
      result.push({
        id,
        englishTitle,
        chineseTitle,
        subTitle: "无",
        paragraph: 0,
        author,
        sourceUrl: url2,
        content: JSON.stringify(
          englishArr.map((item, i) => ({
            originText: item,
            translationText: chineseArr[i],
            bold: false,
          }))
        ),
      });
      let l = 0;
      (n = content.length), (subTitle = "无");
      while (l < n - 1) {
        if ($(`.content p:eq(${l})`).html().indexOf("<strong>") !== -1) {
          subTitle = $(`.content p:eq(${l})`)[0].children[0].children[0].data;
          l++;
          continue;
        }
        const obj = {};
        const originText = $(`.content p:eq(${l})`)[0].children[0].data;
        const translationText = $(`.content p:eq(${l})`)[0].children[2].data;
        const originArr = convertStrToArr(originText);
        const translationArr = convertStrToArr(translationText);
        obj.id = id;
        obj.englishTitle = englishTitle;
        obj.chineseTitle = chineseTitle;
        obj.subTitle = subTitle;
        obj.paragraph = l + 1;
        obj.author = author;
        obj.sourceUrl = url2;
        obj.content = originArr.map((item, i) => ({
          originText: item,
          translationText: translationArr[i],
          bold: false,
        }));
        result.push(obj);
        subTitle = "无";
        l++;
      }
      // 创建文章标题
      creteTitle({ id, title: englishTitle });
      // 循环创建段落
      for (let i = 0; i < result.length; i++) {
        creteParagraph(result[i]);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};
module.exports = {
  crawler,
};
